﻿Imports System.Xml
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.Xml.Serialization
Imports System.Text

Public Class COMAcx

    Public Function creaXML(ByVal AddComa As Coma.ADD_HEADER) As String
        Dim memory_stream As MemoryStream
        Dim xmlNameSpace As XmlSerializerNamespaces
        Dim xmlTextWriter As XmlTextWriter
        Dim xs As XmlSerializer
        Dim stream_reader As StreamReader
        Dim xmlDoc As XmlDocument
        Try
            'SERIALIZAR
            memory_stream = New MemoryStream()
            xmlNameSpace = New XmlSerializerNamespaces()
            xmlNameSpace.Add("", "")
            xmlTextWriter = New XmlTextWriter(memory_stream, Encoding.UTF8)
            xmlTextWriter.Formatting = Formatting.Indented
            xmlTextWriter.Indentation = 4
            xs = New XmlSerializer(GetType(Coma.ADD_HEADER))
            xs.Serialize(xmlTextWriter, AddComa, xmlNameSpace)
            stream_reader = New StreamReader(memory_stream)
            memory_stream.Seek(0, SeekOrigin.Begin)
            xmlDoc = New XmlDocument
            xmlDoc.LoadXml(stream_reader.ReadToEnd())
            'Dim AddendaAMC71 As String = xmlDoc.DocumentElement.OuterXml.Replace("<ComprobanteAddenda>", "").Replace("</ComprobanteAddenda>", "")
            Return xmlDoc.DocumentElement.OuterXml
            xmlTextWriter.Close()
            memory_stream.Close()
            memory_stream.Dispose()
            stream_reader.Close()
            stream_reader.Dispose()
        Catch ex As Exception
            Throw New Exception("Ocurrió un error al generar la Addenda: " & ex.Message)
        Finally
            memory_stream = Nothing
            xmlNameSpace = Nothing
            xmlTextWriter = Nothing
            xs = Nothing
            stream_reader = Nothing
            xmlDoc = Nothing
            AddComa = Nothing
        End Try
    End Function

#Region "Metodos_Auxiliares"
    'Public Function valida_RFC(ByVal sRfc As String) As Boolean
    '    Dim re As Regex = New Regex( _
    '                        "^[a-zA-Z]{3,4}(\d{6})((\D|\d){3})?$", _
    '                        RegexOptions.IgnoreCase)
    '    Dim m As Match = re.Match(sRfc)
    '    If m.Captures.Count = 0 Then
    '        Return False
    '    Else
    '        Return True
    '    End If
    'End Function
    Public Function valida_Correo(ByVal sCorreo As String) As Boolean
        Dim re As Regex = New Regex( _
                            "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@" & _
                            "[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\." & _
                            "[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", _
                            RegexOptions.IgnoreCase)
        Dim m As Match = re.Match(sCorreo)
        If m.Captures.Count = 0 Then
            Return False
        Else
            Return True
        End If
    End Function
    'Private Sub IntegraNodo(ByRef Nodo As XmlNode, ByRef DocXml As XmlDocument, ByVal Val As String, _
    '                        ByVal At As String)
    '    Dim xmlAtt As XmlAttribute
    '    Dim Valor As XmlText
    '    Try
    '        Dim NodGenerico As XmlNode = DocXml.CreateElement("mf", "NEMONICO", "http://www.masteredi.com.mx/masfactura2")
    '        xmlAtt = DocXml.CreateAttribute("nombre")
    '        xmlAtt.Value = At
    '        NodGenerico.Attributes.Append(xmlAtt)

    '        Valor = DocXml.CreateTextNode(Val)
    '        NodGenerico.AppendChild(Valor)
    '        Nodo.AppendChild(NodGenerico)
    '    Catch ex As Exception

    '    End Try
    'End Sub
#End Region
End Class
